﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SilverPOP.Data.Process.OperationUnit;
using SilverPOP.Data.Process.BLL;
using Utilities;
using Helpers;
using SilverPOP.Data.Process.DAL;

namespace SilverPOP.Data.Process
{
    public class UserInteraction
    {
        #region Fields
        String userConsoleInput = String.Empty;
        String databaseName, userName, password;
        bool isInputOk = false;
        public const string SilverPOPSchemaCreationValue = "786_Crt";
        #endregion

        #region Properties
        public OperationMode SelectedOperationMode {get;set;}
        public OperationProcess SelectedProcess {get;set;}
        public long CampaignID { get; set; }
        public String DevConnectionString { get; set; }
        public String DBSchemaCreationInSilverPOP { get; set; }
        #endregion

        #region Constructor
        public UserInteraction() 
        { 
        
        }
        #endregion

        #region  Do User Interaction
        public void DoUserInteraction()
        {
            //Keep the methods calling order sequence. This is very important.
            this.GetOperationMode();
            if (this.SelectedProcess != OperationProcess.SilverPOPDatabaseColumnSchemaCreate)
            {
                this.GetSelectedProcess();
                this.GetConnectionStringIfDevMode();
                this.GetCampaignID();
            }
        }

        #region Get Operatoin Mode
        private void GetOperationMode()
        {
            isInputOk = false;
            userConsoleInput = ReadConsoleInput.ReadInput("Press: 1 for Development Mode"+"\nPress: 2 for Production Mode ");

            while (!isInputOk)
            {
                if (userConsoleInput == SilverPOPSchemaCreationValue)
                {
                    this.SelectedProcess = OperationProcess.SilverPOPDatabaseColumnSchemaCreate;
                    isInputOk = true;
                }

                else if (!RegExUtility.IsNumber(userConsoleInput))
                {
                    userConsoleInput = ReadConsoleInput.ReadInput("Your input is not correct.\n" + "Press: 1 for Development Mode" + "\nPress: 2 for Production Mode ");
                }
                else
                {
                    if (Convert.ToInt32(userConsoleInput) != 1 && Convert.ToInt32(userConsoleInput) != 2)
                    {
                        userConsoleInput = ReadConsoleInput.ReadInput("Your input is not correct.\n" + "Press: 1 for Development Mode" + "\nPress: 2 for Production Mode ");
                    }
                    else if (Convert.ToInt32(userConsoleInput) == 1 || Convert.ToInt32(userConsoleInput) == 2)
                    {
                        this.SelectedOperationMode = (OperationMode)Convert.ToInt32(userConsoleInput);
                        isInputOk = true;
                    }

                }//end main else

            }//end while
            
        }
        #endregion

        #region Get Operation Process
        private void GetSelectedProcess()
        {
             isInputOk = false;
             userConsoleInput = GetUserSelectedProcess();

             while (!isInputOk)
             {
                 if (!RegExUtility.IsNumber(userConsoleInput))
                 {
                     userConsoleInput = GetUserSelectedProcess("Your input is not correct.\n");
                 }
                 else
                 {
                     if (Convert.ToInt32(userConsoleInput) >= 1 && Convert.ToInt32(userConsoleInput) <= 4)
                     {
                         this.SelectedProcess = (OperationProcess) Convert.ToInt32(userConsoleInput);
                         isInputOk = true;
                     }
                     else
                     {
                         userConsoleInput = GetUserSelectedProcess("Your input is not correct.\n");
                     }

                 }//end main else
             }//end while
        }

        private String GetUserSelectedProcess(String errorText = default(String))
        {
            //return ReadConsoleInput.ReadInput(errorText + "Press: 1 for doing SilverPOP Data Sync Process\n" + "Press: 2 for doing SilverPOP Data Update Process\n"
            //                             + "Press: 3 for doing SilverPOP Raw Report Process\n" + "Press: 4 for doing SilverPOP Aggregate Report Process\n");

            return ReadConsoleInput.ReadInput(errorText + "Press: 1 for doing SilverPOP Data Sync Process With Contact List\n" 
                                                        + "Press: 2 for doing SilverPOP Data Sync Process With Out Contact List\n" 
                                                        + "Press: 3 for doing SilverPOP Data Update Process\n"
                                                        + "Press: 4 for doing SilverPOP Raw Report Process\n");
        }

        #endregion

        #region Get Connection String For Development Mode
        private void GetConnectionStringIfDevMode()
        {
            isInputOk = false;

            if (this.SelectedOperationMode == OperationMode.Dev)
            {
               while (!isInputOk)
               {
                   databaseName = ReadConsoleInput.ReadInput("Provide connection string.\n" + "Database Name:");
                   userName = ReadConsoleInput.ReadInput("User Name: ");
                   password = ReadConsoleInput.ReadInput("Password: ");

                   this.DevConnectionString = ConfigHelpers.DEVSQLConnectionStringPart + "Initial Catalog=" + databaseName + ";User ID=" + userName + ";Password=" + password;

                   if (IsConnectionStringValid())
                   {
                       //Assign The Connection String In DAL
                       DAL.DAL.ConnectionString = this.DevConnectionString;
                       break;
                   }
               }
            
            }//end if 
        }

        private bool IsConnectionStringValid()
        {
            try
            {
                Console.WriteLine("Validating connection string ...");
                return DAL.DAL.CheckConnectionString(this.DevConnectionString);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connection string is not correct.");
                return false;
            }

        }
        #endregion

        #region Get Campaign ID
        private void GetCampaignID()
        {
            isInputOk = false;
            userConsoleInput = ReadConsoleInput.ReadInput("Provide Campaign ID: ");

            while (!isInputOk)
            {
                if (!RegExUtility.IsNumber(userConsoleInput))
                {
                    userConsoleInput = ReadConsoleInput.ReadInput("Your input is not correct.\n" + "Provide Campaign ID: ");
                }
                else
                {
                    this.CampaignID = Convert.ToInt64(userConsoleInput);
                    isInputOk = true;
                }
            }//end while

        }
        #endregion



        #endregion



    }//end class
}
